# coding:utf-8

"""
tf.demo07_build_neural_network
Created on 2016/12/9 14:45
@author: GuoYufu
@group : OceanHorn
@contact: OceanHorn@163.com
"""

from tf_demo06_define_addLayer import *
import numpy as np

if __name__ == "__main__":

    x_data = np.linspace(start=-1, stop=1, num=300)[:, np.newaxis]
    noise = np.random.normal(loc=0, scale=0.05, size=x_data.shape)

    y_data = np.square(x_data) - 0.5 + noise

    xs = tf.placeholder(dtype=tf.float32, shape=[None, 1])
    ys = tf.placeholder(dtype=tf.float32, shape=[None, 1])

    layer1_outputs = add_layer(inputs=xs, in_size=1, out_size=10, activation_function=tf.nn.relu)
    prediction_layer_outputs = add_layer(inputs=layer1_outputs, in_size=10, out_size=1, activation_function=None)

    loss = tf.reduce_mean(
        tf.reduce_sum(
            tf.square(ys - prediction_layer_outputs), reduction_indices=[1]
        )
    )
    train_step = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(loss)

    initialize = tf.global_variables_initializer()

    with tf.Session() as session:
        session.run(initialize)

        for i in range(1001):
            session.run(train_step, feed_dict={xs: x_data, ys:y_data})

            if i % 20 == 0:
                print session.run(loss, feed_dict={xs: x_data, ys:y_data})
